package com.example.reflectiondemo;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @program my-project-model
 * @description:
 * @author: lee
 * @create: 2022/09/23 17:12
 */
public class Main {


    @SuppressWarnings("unchecked")
    public static void main(String args[]) throws Exception {
        List<Student> list = new ArrayList<Student>();
        list.add(new Student());
        list = Collections.unmodifiableList(list);
        System.out.println("不可变集合的元素数量：" + list.size());//1
        Field pro = ReflectUtil.getDeclaredField(list, "list");
        pro.setAccessible(true);
        List<Student> modifyerList = (List<Student>) pro.get(list);
        modifyerList.add(new Student());
        System.out.println("不可变集合的元素数量：" + list.size());//2
        //list.add(new Student());//报错java.lang.UnsupportedOperationException
    }
}
